package com.aynu.admin.controller;

import com.aynu.admin.domain.CommonResponse;
import com.aynu.admin.domain.po.TAccount;
import com.aynu.admin.filter.AuthenticationInterceptor;
import com.aynu.admin.service.base.ITAccountService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/account")
public class AccountController {
    @Resource
    private ITAccountService accountService;

    @GetMapping("/list")
    public CommonResponse<List<TAccount>> list() {
        return CommonResponse.success(
                accountService.list(
                        Wrappers.<TAccount>lambdaQuery()
                                .orderByAsc(TAccount::getUsername)
                )
        );
    }

    /**
     *
     * @param account 需要电话、用户名、密码字段
     * @return 响应
     */
    @PostMapping("/add")
    public CommonResponse<Object> add(@RequestBody TAccount account) {
        String userPrincipal = AuthenticationInterceptor.getUserPrincipal();
        return accountService.saveAccount(userPrincipal, account);
    }

    /**
     *
     * @param account 需要手机号和密码其一
     * @return ..
     */
    @PutMapping("/modify")
    public CommonResponse<Object> modify(@RequestBody TAccount account) {
        return accountService.modifyAccount(account);
    }

    /**
     *
     * @param id 需要帐号id
     * @return ..
     */
    @DeleteMapping("/del")
    public CommonResponse<Object> del(@RequestParam("id") String id) {
        return accountService.delAccount(id);
    }
    @GetMapping("/online")
    public CommonResponse<List<TAccount>> online() {
        return accountService.getOnline();
    }
    @DeleteMapping("/offline")
    public CommonResponse<Object> offline(@RequestParam("id") String id) {
        return accountService.offline(id);
    }
}
